package com.winit.openapi.sms;

import java.text.ParseException;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.BpartnerService;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.sms.common.SmsUtils;
import com.winit.openapi.util.DateUtil;
import com.winit.sms.spi.common.vo.PageVo;
import com.winit.sms.spi.storageWarehouse.StorageCostService;
import com.winit.sms.spi.storageWarehouse.command.QueryStorageCostCommand;
import com.winit.sms.spi.storageWarehouse.command.QueryStorageCostDetailCommand;
import com.winit.sms.spi.storageWarehouse.vo.StorageCostDetailVo;
import com.winit.sms.spi.storageWarehouse.vo.StorageCostVo;

/**
 * 卖家对帐单
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/wallet/storageCost")
public class StorageCostController extends BaseController {

    private static Logger      logger = LoggerFactory.getLogger(StorageCostController.class);
    @Resource
    private StorageCostService storageCostService;
    @Resource
    private BpartnerService    bpartnerService;

    /**
     * 分页查询仓储单信息
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/pageStorageCost")
    public String pageStorageCost(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg)
                                                                                                                 throws ParseException {
        QueryStorageCostCommand command = SmsUtils.toJavaObject(requestMsg.getData(), QueryStorageCostCommand.class);
        PageVo pageVo = SmsUtils.SetPageParams(requestMsg.getData());
        command.setPageVo(pageVo);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>仓储单查询>>>>>>>>>>>>>>>>>bpartner为空");
            return ERROR;
        }
        command.setCustomerCose(bpartner.getValue());
        if (command.getCostDateFrom() != null) {
            command.setCostDateFrom(DateUtil.spliceDate2(command.getCostDateFrom()));
        }
        if (command.getCostDateTo() != null) {
            command.setCostDateTo(DateUtil.spliceDate(command.getCostDateTo()));
        }
        try {
            Page<StorageCostVo> pageStatement = storageCostService.pageStorageCost(command);
            Map<String, Object> data = SmsUtils.setPageData(pageStatement);
            responseMsg.setData(data);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }

    /**
     * 分页查询仓储单明细信息
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/pageStorageCostDetail")
    public String pageStorageCostDetail(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg)
                                                                                                                       throws ParseException {
        QueryStorageCostDetailCommand command = SmsUtils.toJavaObject(requestMsg.getData(),
            QueryStorageCostDetailCommand.class);
        PageVo pageVo = SmsUtils.SetPageParams(requestMsg.getData());
        command.setPageVo(pageVo);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>仓储单查询>>>>>>>>>>>>>>>>>bpartner为空");
            return ERROR;
        }
        command.setCustomerCose(bpartner.getValue());
        if (command.getCostDateFrom() != null) {
            command.setCostDateFrom(DateUtil.spliceDate2(command.getCostDateFrom()));
        }
        if (command.getCostDateTo() != null) {
            command.setCostDateTo(DateUtil.spliceDate(command.getCostDateTo()));
        }
        try {
            Page<StorageCostDetailVo> pageStatement = storageCostService.pageStorageCostDetail(command);
            Map<String, Object> data = SmsUtils.setPageData(pageStatement);
            responseMsg.setData(data);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }
}
